package org.dromara.datacheck.controller.jd;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.datacheck.domain.bo.jd.JdSaleReturnOrderBo;
import org.dromara.datacheck.domain.jd.JdSaleReturnOrder;
import org.dromara.datacheck.domain.vo.jd.JdSaleReturnOrderImportVo;
import org.dromara.datacheck.domain.vo.jd.JdSaleReturnOrderVo;
import org.dromara.datacheck.listener.jd.JdSaleReturnOrderImportListener;
import org.dromara.datacheck.service.jd.IJdSaleReturnOrderService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 金蝶-销售退货单
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataCheck/jd/saleReturnOrder")
public class JdSaleReturnOrderController extends BaseController {

    private final IJdSaleReturnOrderService jdSaleReturnOrderService;

    /**
     * 查询金蝶-销售退货单列表
     */
    @SaCheckPermission("dataCheck:saleReturnOrder:list")
    @GetMapping("/list")
    public TableDataInfo<JdSaleReturnOrderVo> list(JdSaleReturnOrderBo bo, PageQuery pageQuery) {
        return jdSaleReturnOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出金蝶-销售退货单列表
     */
    @SaCheckPermission("dataCheck:saleReturnOrder:export")
    @Log(title = "金蝶-销售退货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(JdSaleReturnOrderBo bo, HttpServletResponse response) {
        List<JdSaleReturnOrderVo> list = jdSaleReturnOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "金蝶-销售退货单", JdSaleReturnOrderVo.class, response);
    }

    /**
     * 获取金蝶-销售退货单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataCheck:saleReturnOrder:query")
    @GetMapping("/{id}")
    public R<JdSaleReturnOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(jdSaleReturnOrderService.queryById(id));
    }

    /**
     * 新增金蝶-销售退货单
     */
    @SaCheckPermission("dataCheck:saleReturnOrder:add")
    @Log(title = "金蝶-销售退货单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody JdSaleReturnOrderBo bo) {
        return toAjax(jdSaleReturnOrderService.insertByBo(bo));
    }

    /**
     * 修改金蝶-销售退货单
     */
    @SaCheckPermission("dataCheck:saleReturnOrder:edit")
    @Log(title = "金蝶-销售退货单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody JdSaleReturnOrderBo bo) {
        return toAjax(jdSaleReturnOrderService.updateByBo(bo));
    }

    /**
     * 删除金蝶-销售退货单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataCheck:saleReturnOrder:remove")
    @Log(title = "金蝶-销售退货单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(jdSaleReturnOrderService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "金蝶-销售退货单导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        // 每次导入之前清空表里的数据
        List<JdSaleReturnOrder> jdOtherEnterWarhouseOrderList = jdSaleReturnOrderService.queryAll();
        if(CollUtil.isNotEmpty(jdOtherEnterWarhouseOrderList)){
            jdSaleReturnOrderService.deleteAll();
        }
        // 解析Excel文件并处理导入逻辑
        ExcelResult<JdSaleReturnOrderImportVo> result = ExcelUtil.importExcel(file.getInputStream(), JdSaleReturnOrderImportVo.class, new JdSaleReturnOrderImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 查询金蝶-销售退货单列表
     * 场景分类：寄售-寄售换货-售后单(猫超售后店铺，进口猫超售后店铺)
     * 业务查询条件：根据sale_return_customer退货客户为猫超寄售售后、进口超市售后店铺货询
     */
    @SaCheckPermission("dataCheck:saleReturnOrder:list")
    @GetMapping("/list-by-sale-return-customer1")
    public TableDataInfo<JdSaleReturnOrderVo> listBySaleReturnCustomer1(JdSaleReturnOrderBo bo, PageQuery pageQuery) {
        bo.setSaleReturnCustomerList(Arrays.asList("猫超寄售售后","进口超市售后店铺","京东自营-售后店铺"));
        return jdSaleReturnOrderService.queryPageListBySaleReturnCustomer1(bo, pageQuery);
    }
}
